package com.edge.pbx.view.delay;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.edge.pbx.view.PbxCircleSeekBar;

import com.edge.pbxdsp.R;
import com.qztech.utils.YzDebug;

public class PbxDelayContentLayout extends ViewGroup {
    public PbxDelayContentLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    private int  mHalfUsedWidth;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int width = MeasureSpec.getSize(widthMeasureSpec);
        final int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(width, height);

        View view = null;
        view = findViewById(R.id.pbx_delay_output_select_layout_wrapper);
        int centerViewSize = (int) Math.min(width/2.8f, height*0.8f);
        int spec = 0;
        if (view != null){
            spec = MeasureSpec.makeMeasureSpec(centerViewSize, MeasureSpec.EXACTLY);
            view.measure(spec, spec);
        }
        mHalfUsedWidth = (int) ((width - centerViewSize)/2);
        int seekbarSize = (int) Math.min(mHalfUsedWidth/2.1f, height/2.1f);
        spec = MeasureSpec.makeMeasureSpec(seekbarSize,MeasureSpec.EXACTLY);
        //seekbar
        for (int i = 0; i < 8; i++){
            view = getChildAt(i+1);// 1 add centerview
            if (view == null)continue;
            view.measure(spec,spec);

        }
        int tw = (int) (seekbarSize*0.5f);
        int th = (int) (seekbarSize*0.2f);
        spec = MeasureSpec.makeMeasureSpec(tw, MeasureSpec.EXACTLY);
        int spech = MeasureSpec.makeMeasureSpec(th, MeasureSpec.EXACTLY);
        for (int i = 0; i < 8; i++) {
            view = getChildAt(i+9);
            if (view == null)continue;
            view.measure(spec, spech);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        YzDebug.d("onTouchEvent:" + event.getAction());
        return true;

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int width = r - l;
        final int height = b - t;
        View view = null;
        view = findViewById(R.id.pbx_delay_output_select_layout_wrapper);
        int vw,vh, btnW,btnH;
        if (view != null){
            vw = view.getMeasuredWidth();
            vh = view.getMeasuredHeight();
            l = (width - vw)>>1;
            t = (height - vh) >> 1;
            view.layout(l, t, l+vw, t +vh);
        }
        view = getChildAt(1);
        vw = view.getMeasuredWidth();
        vh = view.getMeasuredHeight();
        int vpadding = (height - vh *2)/2;
        int hpadding = (mHalfUsedWidth - vw * 2)/3;

        int tmp =  width - mHalfUsedWidth;

        for (int i = 0; i < 8; i++) {
            view = getChildAt(i + 1);//add centerview
            if (view == null)continue;
            if (i < 4){
                l = hpadding +(hpadding+vw)*(i/2)  ;
            }else {
                l = hpadding +(hpadding+vw)*(i/2 - 2) + tmp  ;
            }

            t = vpadding +(vpadding +vh)*(i%2);
            view.layout(l,t,l+vw,t+vh);

            view = getChildAt(i+9);
            if (view == null)continue;
            btnW = view.getMeasuredWidth();
            btnH = view.getMeasuredHeight();
            l = l + (vw -btnW)/2;
            t = (int) (t + vh *0.45f);
            view.layout(l, t, l + btnW, t + btnH);
        }

    }

//    @Override
//    protected void onFinishInflate() {
//        super.onFinishInflate();
//        View view = null;
//        final String fmt1 = "%.1fms";
//        final PbxCircleSeekBar.OnCircleSeekBarTextListener listener = value ->String.format(fmt1,value/10f);
//        for (int i = 0, count = getChildCount(); i < count; i++){
//            view = getChildAt(i);
//            if (view instanceof PbxCircleSeekBar){
//                ((PbxCircleSeekBar)view).setOnCircleSeekBarTextListener(listener);
//            }
//        }
//    }
}
